<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title data-i18n="resources.title_closestFacilitiesService"></title>
    <style>
        #toolbar {
          position: absolute;
          top: 50px;
          right: 10px;
          width: 320px;
          text-align: center;
          z-index: 1000;
          border-radius: 4px;
        }
    </style>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
<div id="toolbar" class="panel panel-primary">
  <div class='panel-heading'>
      <h5 class='panel-title text-center' data-i18n="resources.text_closestFacilitiesService"></h5></div>
  <div class='panel-body content'>
      <div class='panel'>
          <div class='input-group'>
              <span class='input-group-addon' data-i18n="resources.text_weightField"></span>
              <select class='form-control' id='edgeWeightNames'>
                  <option>length</option>
                  <option>time</option>
              </select>
          </div>
      </div>
      <div class='panel'>
          <div class='input-group'>
              <span class='input-group-addon' data-i18n="resources.text_turnField"></span>
              <select class='form-control' id='turnNodeWeightNames'>
                  <option>TurnCost</option>
              </select>
          </div>
      </div>
      <input type="button" class="btn btn-default" data-i18n="[value]resources.text_input_value_eventPoint" onclick="selectEventPoint()"/>&nbsp;
      <input type="button" class="btn btn-default" data-i18n="[value]resources.text_input_value_submit" onclick="findClosestFacilities()"/>&nbsp;
      <input type="button" class="btn btn-default" data-i18n="[value]resources.text_input_value_clear" onclick="clearElements(true)"/>
  </div>
</div>
<div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
<script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
<script type="text/javascript" include="leaflet-geoman" src="../../dist/leaflet/include-leaflet.js"></script>
<script type="text/javascript">
    var host = window.isLocal ? window.server : "https://iserver.supermap.io";
    var map, findClosetFacilitiesService, findClosetFacilitiesParameter, resultLayer, eventPoint,
        baseUrl = host + "/iserver/services/map-changchun/rest/maps/长春市区图",
        serviceUrl = host + "/iserver/services/transportationanalyst-sample/rest/networkanalyst/RoadNet@Changchun";
    map = L.map('map', {
        crs: new L.supermap.CRS.NonEarthCRS({
            bounds: L.bounds([48.4, -7668.25], [8958.85, -55.58]),
            origin: L.point(48.4, -55.58)
        }),
        center: [-3600, 4700],
        maxZoom: 18,
        zoom: 2
    });
    new L.supermap.TiledMapLayer(baseUrl, {noWrap: true})
        .addTo(map)
        .on('load', function () {
            init();
        });
    
    function init() { 
        //添加设施点
        var marker1 = L.marker([-3500, 2500]).addTo(map);
        marker1.bindPopup(resources.text_facilityPoint1);
        var marker2 = L.marker([-2500, 5500]).addTo(map);
        marker2.bindPopup(resources.text_facilityPoint2);
        var marker3 = L.marker([-4000, 7000]).addTo(map);
        marker3.bindPopup(resources.text_facilityPoint3);
    }

    function selectEventPoint() {
        clearElements(true);
        map.pm.enableDraw('Marker', {
          tooltips: false,
          markerStyle: {
            icon: L.icon({
              iconUrl: '../img/marker.png',
              iconSize: [44, 30]
            })
          }
        });
        map.on('pm:create', function (e) {
          if (e.layer) {
            eventPoint = e.layer;
            map.pm.disableDraw();
          }
        });
    }

    function findClosestFacilities() {
        clearElements();
        if (!eventPoint) {
          return widgets.alert.showAlert(resources.msg_eventPoint, false);
        }  
        //创建最近设施分析服务实例
        findClosetFacilitiesService = new L.supermap.NetworkAnalystService(serviceUrl);
        //创建最近设施分析参数实例
        var resultSetting = new L.supermap.TransportationAnalystResultSetting({
            returnEdgeFeatures: true,
            returnEdgeGeometry: true,
            returnEdgeIDs: true,
            returnNodeFeatures: true,
            returnNodeGeometry: true,
            returnNodeIDs: true,
            returnPathGuides: true,
            returnRoutes: true
        });
        var analystParameter = new L.supermap.TransportationAnalystParameter({
            resultSetting: resultSetting,
            turnWeightField: "TurnCost",
            weightFieldName: "length"  //length,time
        });
        findClosetFacilitiesParameter = new L.supermap.FindClosestFacilitiesParameters({
            //事件点,必设参数
            event: eventPoint.getLatLng(),
            //要查找的设施点数量。默认值为1
            expectFacilityCount: 1,
            //设施点集合,必设
            facilities: [L.latLng(-3500, 2500), L.latLng(-2500, 5500), L.latLng(-4000, 7000)],
            isAnalyzeById: false,
            parameter: analystParameter
        });
        //进行查找
        findClosetFacilitiesService.findClosestFacilities(findClosetFacilitiesParameter).then(function (serviceResult) {
            var rersult = serviceResult.result;
            rersult.facilityPathList.map(function (result) {
                resultLayer = L.geoJSON(result.route).addTo(map);
            });
        });
    }

    function clearElements(clearPoint) {
      map.pm.disableDraw();
      map.off('pm:create');
      resultLayer && resultLayer.clearLayers();
      if (eventPoint && clearPoint) {
        eventPoint.pm.remove();
        eventPoint = null;
      }
    }
</script>
</body>
</html>